package com.practice.service.impl;

import com.practice.dao.BaseDao;
import com.practice.domain.BaseDomain;
import com.practice.exception.PracticeException;
import com.practice.service.BaseService;

/**
 * Created by Administrator on 2016/8/20.
 */
public class BaseServiceImpl implements BaseService {
  protected <T extends BaseDomain<T>> T detail(Long id, BaseDao<T> dao, String label) throws PracticeException {
    if (null == id || id < 1) {
      throw new PracticeException("id不能为空或小于0！");
    }

    T result = dao.detail(id);

    if (null == result) {
      throw new PracticeException("不存在id为" + id + "的" + label + "！");
    }

    return result;
  }

  protected <T> void validateEmpty(T item, String label) throws PracticeException {
    if (isEmpty(item)) {
      throw new PracticeException(label + "不能为空！");
    }
  }

  protected <T> boolean isEmpty(T item) {
    return null == item || ((item instanceof String) && item.equals(""));
  }

  protected <T> boolean isNotEmpty(T s) {
    return null != s && !(s instanceof String && ("".equals(s)));
  }
}
